1. GenAI

In this workshop, you will explore the capabilities of WCA for GP (GenAI), Creating sample applications which will be beneficial for implementation perspective.

Goals
  • Understand the Overview of GenAI and Tooling.

  • Understand Prompt Engineering.

  • Risk Management.

  • Live Code Lab Sessions.

2. What is GenAI?

Generative AI is a Software which can use Large Language Models (LLMs) which are pre-trained with existing datasets and can generate some artifacts out of those existing datasets based on the neural networks.

genai

2.1. History of GenAI

History_GenAI

2.1.1. Other LLMs

Product

Company

WCA for GP

IBM

ChatGPT

OpenAI, Bing, CoPilot, Microsoft

Gemini, PaLM2

Google

Llama2

Meta

Cohere

Coral

Grok

xAI

Titan

Amazon

GalaxyAI

Samsung

Blackbox

Blackbox AI

2.2. Why GenAI is Important?

Pros

Cons

Automation

Bias

Increases Productivity

Accuracy

Faster Product Development

Transparency of the trained data

Enhances personalized customization

Copyright/Data Privacy

2.3. Where GenAI is Placed?

  • Artificial intellegence is a wider term, within AI there is machine learning which comprises of Supervised Machine learning, Unsupervised Machine Learning and re-inforced machine learning, and within machine learning there is deeplearning which is a neural network concept and it talks about relationships and finally GenAI is a smaller subset of Deeplearning.

deepai

2.3.1. What is WCA for GP?

IBM Watsonx Code Assistant is an enterprise-grade AI code generation tool1. It leverages generative AI to accelerate development while maintaining the principles of trust, security, and compliance at its core1.

Here are some key features of IBM Watsonx Code Assistant:

  • Code Generation: It can generate net new code with applicable syntax from natural language requests.

  • Code Matching: It provides transparency through visibility into the potential origin of generated code recommendations.

  • Code Modernization: It can translate code from one language to another or refactor legacy code.

Developers and IT Operators can speed up application modernization efforts and generate automation to rapidly scale IT environments. Watsonx Code Assistant is powered by the IBM Granite foundation models that include state-of-the-art large language models designed for code1. These models are geared to help IT teams create high-quality code using AI-generated recommendations based on natural language requests or existing source code.

In summary, IBM Watsonx Code Assistant is a powerful tool that leverages AI to assist developers in generating and modernizing code, increasing productivity, and reducing errors1.

2.4. GenAI Review Architecture

  • Most of the LLMs are built and pre-trained with datasets, they generate the desired output based on the prompts. So, it will have a UI Layer and Chat Prompts to get the desired results.

aiarchitecture

3. What is Prompt Engineering?

Bridge between LLM and Practical Language based tasks.

prompt
promptibm

3.1. Zero Shot Learning

  • ZSL is a machine learning scenario where an AI model is trained to recognize and categorize objects or concepts without having seen any examples of those categories or concepts during training.

3.2. RAG (Retrieval Augmented Generation)

  • It is type of Few Shot Learning where you involve the domain specific knowledge and get the response. Mainly focuses on Content Grounding.

rag

3.3. Chain-of-thought (Few Short Learning)

  • It is all about taking the bigger task of arriving at a response, breaking it down into multiple sections, and then combining the results of all those multiple sections and coming with the final answer. Providing examples to specific context and getting the final result.

cots

3.4. React (Few Short Learning)

  • It falls under the category of Few Short learning, in the COT technique, we are reasoning through the steps and arriving at the response, but in React it is not only reasoning, it is acting based on what else is necessary to arrive at the best response.

react

3.5. DSP (Directional Stimulus Prompting)

  • It extracts the specific information the LLMs. We can provide a hint to get a desired output. It is very simple technique, but it works very effectively to get a specific output.

dsp

3.6. One Shot Learning

  • In one-shot learning, a deep learning model is given only one image to make a split-second decision (usually a yes or no answer). Training data: It involves a single labeled example per class.

3.7. Hallucinations

  • Hallucinations are where you get essentially false results out of a large language model, because the LLM’s are predominantly trained on the internet data which could be conflicting, conflicting information, misinterpretation and so on.

4. Risk Management

4.1. Data Privacy & 3P Security

  • Using a GenAI code assistant that has not been approved may expose the propretery code to third parties.

4.2. Ethics and Reputation

  • Sometimes for Developers data may be biased, causing the output generated having the malicious content (eg. doxing, hatespeech)

4.3. AI Behavioural Vulnerabilities

  • AI Generated code may not be correct or complete, often due to model limitations such as lack of broader context or unclear requirements in the prompt.

4.4. Insecure Code Generation & Threat Actor Evolution

  • Generated code may be having security vulnerabilities which might not have adopted the best practices for secure and robust coding. DDoS, SQL Injections are more likely to occur.

  • Concerns regarding intellectual property and potential copyright infringement.

5. Risk Mitigations

5.1. Comply with IBM’s GenAI Guidelines

  • IBM’s current guidelines prohibits usage of unapproved AI tools Only use WCA for GP to assist you in software development

5.2. Setup Automation to Identify and Capture Risks

  • Setup mature DevSecOps pipeline

  • Peer review process and CI pipeline approval mandatory before PR merge

  • Setup IDE with packages and pre-push hooks to manage risk e.g., EsLint, Gitleaks pre-push hook

5.3. Review the code for any potential risks

  • Manual review pre-integration.

  • Take steps to mitigate the risk based on the review.

5.4. Report the risks/threats

  • Report to the risk to the security and compliance team

  • Proactively, highlight the impact and challenges in detail

Note
Read the organizations security guidelines and policies for using Gen Al, reach out to the security and risk team when in doubt

5.5. Build Responsible AI

responsibleai

5.6. Build Mature DevSecOps Pipeline for capture risks

devsecops

5.7. AI-Generated Code Challenges can be mitigated with proper code review

5.7.1. Context Sensitivity

Al-generated code may not match internal coding standards, like naming conventions or design patterns

5.7.2. Code Efficiency

Al code might be correct but may not be optimal, needing extra work for efficiency ==== Antipating Failures Al-generated code may miss edge cases or error handling, leading to potential failures

5.7.3. Dependency Management

Al suggestions may use outdated libraries, causing compatibility issues

5.7.4. AI Hallucinations

Al might produce code that seems correct but is incorrect or nonsensical

Note
While leveraging Al for code generation can be beneficial, it is crucial to adhere to the approved tools and practices at IBM. Only approved tools (like WCA for GP) should be used to ensure consistency, security,and compliance with IBM standards and policies

6. Documentation

  • WCA for GP supports most documentation use cases Response quality will decline for more complex and vague prompts

  • Good prompt engineering is key.

Documentation Use Cases

Examples

Create documentation for method/function

Document the calculateTotalprice method in the ShoppingCart class using Javadoc for Java code.

Create documentation for class/Component

Generate documentation for the paymentprocessor using JSDoc for Javascript.

Generate Explaination for selected code snipppets

Provide explainations for the selected code snippets related to OrderProcessing module.

Ask Open-ended Questions

Explain the differnt design patterns in Java

Ask Specific Questions re-code snippets

Explain the error handling mechanisms used in the validatePassword function.

7. Unit Testing

  • WCA for GP supports most testing use cases Response quality will decline for more complex and vague prompts

Testing Usecases

Examples

Create unit test - method/function

Using JTest the calculateTotalPrice method in the ShoppingCart class to ensure accurate total price calculations.

Create unit test - class/component

Develop unit tests in Jest for the PaymentProcessor class to verify payment method processing.

Negative Unit test/Error handling

Generate negative unit tests in Pytest for the validatePassword function to handle invalid passwords.

Testing functional capabilities

Implement Junit tests to validate functional capabilities.

8. Code Lab Sessions (Usecases)

Note
We have executed in our WCA VS Code Plugin and Generated the results.
We have catogarized into multiple usecases.
  1. Coding Usecase - How WCA serve as a coding assistant?

  2. Unit Testing Usecase - How WCA helps in creating Unit Testing?

  3. Documentation Usecase - How to use WCA for creating effective documentation?

8.1. Develop a sample code with java project structure. (Usecase - 1)

WCA@IBM generated code in this programming language is not approved for use within IBM products.

Sure! Here's an example of a simple Java project structure:

// Assisted by WCA@IBM
// Latest GenAI contribution: ibm/granite-20b-code-instruct-v2
src/
  main/
    java/
      App.java
    resources/
      data.csv
  test/
    java/
      AppTest.java
    resources/
      test_data.csv
pom.xml
README.md

This project has two main directories: src/main and src/test. The src/main directory contains the source code for the application, while the src/test directory contains the unit tests for the application.

The src/main/java directory contains the main application code, which in this case is a single file named App.java. The src/main/resources directory contains any data files that the application needs to run, such as CSV files.

The src/test/java directory contains the unit tests for the application, which in this case is a single file named AppTest.java. The src/test/resources directory contains any test data files that are needed to run the unit tests.

The pom.xml file is the Maven Project Object Model (POM) file, which is used to manage the dependencies and build settings for the project.

The README.md file is a Markdown file that contains information about the project, including any installation or usage instructions.

8.1.1. Can you create a App.java class & AppTest.java class?

Sure, here's an example of how I would create a basic Java project structure using the App.java and AppTest.java classes:

// Assisted by WCA@IBM
// Latest GenAI contribution: ibm/granite-20b-code-instruct-v2
// App.java
package com.example;

public class App {

    public static void main(String[] args) {
        System.out.println("Hello, world!");
    }
}

AppTest.java

// Assisted by WCA@IBM
// Latest GenAI contribution: ibm/granite-20b-code-instruct-v2
// AppTest.java
package com.example;

import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.*;

class AppTest {

    @Test
    void testMain() {
        String expected = "Hello, world!";
        App app = new App();
        String actual = app.main();
        assertEquals(expected, actual);
    }
}

This creates a basic Java project structure with two classes: App.java and AppTest.java. The App.java class defines a main method that prints "Hello, world!" to the console when executed. The AppTest.java class defines a testMain method that verifies that the main method returns "Hello, world!" when executed.

8.2. Develop a sample code with java project structure with Mavan build tool and by adding Spring-Boot actutator and Web module as a dependency. (Usecase - 2)

WCA@IBM generated code in this programming language is not approved for use within IBM products.

Sure, here's an example of how I would create a basic Java project structure using Maven build tool and add Spring Boot Actuator and Web modules as dependencies:

// Assisted by WCA@IBM
// Latest GenAI contribution: ibm/granite-20b-code-instruct-v2
<!-- pom.xml -->
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.example</groupId>
    <artifactId>demo</artifactId>
    <version>1.0-SNAPSHOT</version>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
            <version>2.6.7</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <version>2.6.7</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <version>2.6.7</version>
            </plugin>
        </plugins>
    </build>

</project>


// Assisted by WCA@IBM
// Latest GenAI contribution: ibm/granite-20b-code-instruct-v2
// App.java
package com.example;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class App {

    public static void main(String[] args) {
        SpringApplication.run(App.class, args);
    }
}


// Assisted by WCA@IBM
// Latest GenAI contribution: ibm/granite-20b-code-instruct-v2
// AppTest.java
package com.example;

import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;

import static org.junit.jupiter.api.Assertions.*;

@SpringBootTest
class AppTest {

    @Test
    void contextLoads() {
    }
}

This creates a basic Java project structure with two classes: App.java and AppTest.java. The pom.xml file defines the project dependencies, including Spring Boot Actuator and Web modules. The App.java class uses the SpringApplication class to start the application. The AppTest.java class verifies that the application starts successfully.

8.2.1. How to run the above project?

WCA@IBM generated code in this programming language is not approved for use within IBM products.

To run the above project, you can follow these steps:

1. Install JDK 8 or higher on your system.
2. Install Maven on your system.
3. Clone or download the project files from GitHub.
4. Navigate to the project directory in your terminal or command prompt.
5. Run the following command to build the project:

# Assisted by WCA@IBM
# Latest GenAI contribution: ibm/granite-20b-code-instruct-v2
mvn clean install

6. Once the build completes, run the following command to start the application:

# Assisted by WCA@IBM
# Latest GenAI contribution: ibm/granite-20b-code-instruct-v2
java -jar target/demo-1.0-SNAPSHOT.jar

7. Open your web browser and navigate to http://localhost:8080/actuator/health to check if the application is running correctly.

That's it! You have now created a basic Java project structure using Maven build tool and added Spring Boot Actuator and Web modules as dependencies.

8.3. Develop an ansible script for installing K8s Cluster using Kubeadm (Usecase - 3)

WCA@IBM generated code in this programming language is not approved for use within IBM products.

# Assisted by WCA@IBM
# Latest GenAI contribution: ibm/granite-20b-code-instruct-v2
Here is an example Ansible playbook that can be used to install a Kubernetes cluster using Kubeadm:

---
- hosts: all
 become: yes
 tasks:
   - name: Update apt cache
     apt: update_cache=yes cache_valid_time=600

   - name: Install required packages
     apt: name={{ item }} state=present
     with_items:
       - apt-transport-https
       - ca-certificates
       - curl
       - software-properties-common
       - python3-pip

   - name: Add Kubernetes repository
     shell: |
       curl -s https://packages.cloud.google.com/apt/doc/apt-key.gpg | apt-key add -
       echo "deb http://apt.kubernetes.io/ kubernetes-xenial main" >> /etc/apt/sources.list

   - name: Install Kubernetes components
     apt: name={{ item }} state=present
     with_items:
       - kubelet
       - kubeadm
       - kubectl

   - name: Start kubelet service
     service: name=kubelet state=started enabled=yes

   - name: Initialize Kubernetes cluster
     shell: kubeadm init --pod-network-cidr=192.168.0.0/16

   - name: Copy kubeconfig file to user home directory
     fetch: src=/etc/kubernetes/admin.conf dest=/home/user/.kube/config flat=yes

   - name: Install Weave Net network plugin
     shell: kubectl apply -f "https://cloud.weave.works/k8s/net?k8s-version=$(kubectl version | base64 | tr -d '\n')"

This playbook installs the necessary packages, adds the Kubernetes repository, installs the Kubernetes components, starts the kubelet service, initializes the Kubernetes cluster, fetches the kubeconfig file, and installs the Weave Net network plugin.

Note that this is just an example and you may need to customize it based on your specific environment and requirements.

8.3.1. Develop an ansible script for installing k8s cluster using kubeadm with Calico as a network plugin.

WCA@IBM generated code in this programming language is not approved for use within IBM products.

# Assisted by WCA@IBM
# Latest GenAI contribution: ibm/granite-20b-code-instruct-v2
Sure, I'd be happy to help! Here's an example Ansible playbook that can be used to install a Kubernetes cluster using Kubeadm with Calico as the network plugin:

---
- hosts: all
 become: yes
 tasks:
   - name: Update apt cache
     apt: update_cache=yes cache_valid_time=600

   - name: Install required packages
     apt: name={{ item }} state=present
     with_items:
       - apt-transport-https
       - ca-certificates
       - curl
       - software-properties-common
       - python3-pip

   - name: Add Kubernetes repository
     shell: |
       curl -s https://packages.cloud.google.com/apt/doc/apt-key.gpg | apt-key add -
       echo "deb http://apt.kubernetes.io/ kubernetes-xenial main" >> /etc/apt/sources.list

   - name: Install Kubernetes components
     apt: name={{ item }} state=present
     with_items:
       - kubelet
       - kubeadm
       - kubectl

   - name: Start kubelet service
     service: name=kubelet state=started enabled=yes

   - name: Initialize Kubernetes cluster
     shell: kubeadm init --pod-network-cidr=192.168.0.0/16

   - name: Copy kubeconfig file to user home directory
     fetch: src=/etc/kubernetes/admin.conf dest=/home/user/.kube/config flat=yes

   - name: Install Calico network plugin
     shell: kubectl apply -f https://docs.projectcalico.org/manifests/calico.yaml

This playbook installs the necessary packages, adds the Kubernetes repository, installs the Kubernetes components, starts the kubelet service, initializes the Kubernetes cluster, fetches the kubeconfig file, and installs Calico as the network plugin.

Note that this is just an example and you may need to customize it based on your specific environment and requirements.

8.4. Develop a shell script code to send slack notifications to the cp-delivery-status channel? (Usecase - 4)

#!/bin/bash

# Slack webhook URL
webhook_url="YOUR_SLACK_WEBHOOK_URL"

# Channel to post the message to
channel="#cp-delivery-status"

# Message to be sent
message="Hello from my script!"

# Send the request to Slack
curl -X POST -H 'Content-type: application/json' --data '{"channel":"'"$channel"'","text":"'"$message"'"}' "$webhook_url"

8.5. Can you create a Jenkins slack notification configuration document in MD format? (Usecase - 5)

// Assisted by WCA@IBM
// Latest GenAI contribution: ibm/granite-20b-code-instruct-v2
---
title: "Jenkins Slack Notification Configuration"
date: 2023-04-18T19:01:17+05:30
draft: false
weight: 6
ShowToc: true
TocOpen: true
tags: ["jenkins", "slack", "notification"]
categories: ["jenkins"]
cover:
    image: "/images/jenkins-slack-notification/jenkins-slack-notification.png"
---

## Introduction

In this guide, we will learn how to configure Jenkins to send build notifications to Slack channels. We will use the [Slack plugin](https://plugins.jenkins.io/slack/) for Jenkins to send messages to Slack channels.

## Prerequisites

To follow this guide, you will need:

1. A Jenkins server installed and configured. If you don't have one, you can set up a new Jenkins server using the [official documentation](https://www.jenkins.io/doc/book/installing/).
2. A Slack workspace and a channel where you want to receive build notifications. If you don't have one, you can create a new workspace and channel using the [Slack website](https://slack.com/).
3. The [Slack plugin](https://plugins.jenkins.io/slack/) installed in your Jenkins server. You can install it from the Manage Jenkins > Manage Plugins page in your Jenkins web interface.

## Step 1: Configure the Slack Plugin

1. In your Jenkins web interface, go to Manage Jenkins > Configure System > Slack.
2. In the Slack section, click Add Slack Site.
3. Enter a name for your Slack site (e.g., MyCompany Slack).
4. In the Slack Webhook URL field, enter the webhook URL of your Slack workspace. To obtain the webhook URL, follow these steps:
    1. Go to your Slack workspace and select the channel where you want to receive build notifications.
    2. Click Integrations > Incoming Webhooks.
    3. Click Add New Webhook to Workspace.
    4. Select the channel where you want to receive build notifications.
    5. Click Authorize.
    6. Copy the webhook URL from the Webhook URLs section.
5. Click Save.

## Step 2: Configure the Jenkins Job

1. In your Jenkins web interface, create a new job or open an existing job.
2. In the Build Triggers section, click Add Trigger.
3. Select Slack Notifications.
4. In the Slack Notifications section, select the Slack site that you configured in Step 1.
5. In the Channel field, enter the name of the channel where you want to receive build notifications (e.g., #build-notifications).
6. In the Message field, enter the message that you want to send to the channel. You can use the following variables in your message:

    - `${JOB_NAME}`: Name of the Jenkins job.
    - `${BUILD_NUMBER}`: Build number of the Jenkins job.
    - `${BUILD_STATUS}`: Status of the Jenkins job (e.g., SUCCESS, FAILURE, etc.).
    - `${BUILD_URL}`: URL of the Jenkins job.

7. Click Advanced... to customize the behavior of the Slack Notifications trigger. For example, you can choose whether to notify on success, failure, or both.
8. Click Save.

That's it! Now, whenever the Jenkins job runs, it will send a notification to the specified Slack channel.

8.6. Can you create a registry.conf file using OpenShift API Objects and definitions? (Usecase - 6)

Note
Ground the Context and rephrase the prompt as "can you create a registry.conf file using above example
WCA@IBM generated code in this programming language is not approved for use within IBM products.

Sure! Here's an example of the registry.conf file that you requested:

// Assisted by WCA@IBM
// Latest GenAI contribution: ibm/granite-20b-code-instruct-v2
unqualified-search-registries = ["docker-na-public.artifactory.swg-devops.com/hyc-cloud-private-daily-docker-local/ibmcom"]
[[registry]]
  prefix = "icr.io/cpopen"
  location = "docker-na-public.artifactory.swg-devops.com/hyc-cloud-private-daily-docker-local/ibmcom"

[[registry.mirror]]
  location = "docker-na-public.artifactory.swg-devops.com/hyc-cloud-private-daily-docker-local/ibmcom"

[[registry]]
  prefix = "icr.io/cpopen/cpfs"
  location = "docker-na-public.artifactory.swg-devops.com/hyc-cloud-private-daily-docker-local/ibmcom"

[[registry.mirror]]
  location = "docker-na-public.artifactory.swg-devops.com/hyc-cloud-private-daily-docker-local/ibmcom"

8.7. Can you explain this logic (Documentation)? (Usecase - 7)

slack
Sure, I'd be happy to explain the code!

Here's an overview of what it does:

It checks if the current branch is main. If it is, it uses the production Slack webhook URL. If it's not, it uses the staging Slack webhook URL.
It constructs a JSON payload for the Slack message using the curl command. The payload contains a Slack block that displays the job name, repository, branch, build ID, and status (success or failure).
It sends the request to the Slack webhook URL using the curl command.
Overall, this code is used to send notifications to the appropriate Slack channel based on the current branch.

8.8. Can you generate Unit Test case for this below scenario? (Usecase - 8)

unittest
# Unit tests for the Slack notification script
## Test 1: Check if the correct webhook URL is used based on the branch

// Assisted by WCA@IBM
// Latest GenAI contribution: ibm/granite-20b-code-instruct-v2
if [[ "$CI_BASE_BRANCH" == 'main' ]]; then #the main builds always deliver to `main`
    # shellcheck disable=SC2269
    SLACK_WEBHOOK_URL=$SLACK_WEBHOOK_URL
else
    SLACK_WEBHOOK_URL=$SLACK_STAGING_WEBHOOK_URL #delivers to the staging slack channel #cp-delivery-main-test
fi

HTTP_RESPONSE=$(curl --silent --write-out "HTTPSTATUS:%{http_code}" \
    --header "Content-type: application/json" \
    --request POST \
    --data \
    "{
    \"blocks\":[
        {
          \"type\":\"section\",
          \"text\":{
              \"type\":\"mrkdwn\",
              \"text\":\"*${TRAVIS_JOB_NAME} from ${TRAVIS_REPO_SLUG}@${TRAVIS_BRANCH} *\n*Build #<${base_travis_url}/builds/${TRAVIS_BUILD_ID}|$TRAVIS_BUILD_ID>\n $message\"
          }
        }
    ]
  }" \
  "$SLACK_WEBHOOK_URL")

This test checks if the correct webhook URL is used based on the branch. If the build is on the main branch, it uses the production webhook URL. Otherwise, it uses the staging webhook URL. The test also verifies that the HTTP request was successful by checking the HTTP status code returned by the server.

9. Conclusions

  • So we have learned the overview of GenAI and use the effective prompting techniques for a better productivity.

10. References

  1. Slides: https://ibm.ent.box.com/folder/257790890004

  2. YourLearning Modules:https://yourlearning.ibm.com/activity/PLAN-C2D21AFFEE66

  3. WCA for GP Plugin Docs: https://w3.ibm.com/w3publisher/orai-microsite/wca-at-ibm

11. Summary

  1. We have now learned the overview of the GenAI

  2. We have now learned the overview of the Prompting Techniques.

  3. We have now learned the overview of Risk Management and mitigation.

  4. We have now learned the overview of Documentation.

  5. We have now learned the overview of Unit Testing.